import table from '@/components/dt/table/render.js'
import dateOpt from '@/biz/option/date.js'
import Opt from '@/components/dt/info/Opt.js'
import CustomerMgr from '@/biz/customer/customerOpt.js'

import nameOpt from '@/biz/option/name.js'

let _this = null
class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      {
        field: 'activityName',
        minWidth: 200,
        title: '活动名',
        header: {
          filter: {
            title: '活动名',
            key: 'activityName',
            placeholder: '请输入活动名',
            input: {}
          }
        }
      },
      {
        title: '渠道',
        field: 'channelName',
        minWidth: 120,
        header: {
          filter: {
            title: '来源渠道',
            key: 'channelId',
            placeholder: '请选择来源渠道',
            load: (o) => {
              CustomerMgr.getChannel().then((res) => {
                o.select.options = res
              })
            },
            select: {
              options: [],
              multiple: false,
              filterable: true
            }
          }
        }
      },
      {
        title: '创建时间',
        field: 'createTime',
        minWidth: 180,
        header: {
          filter: dateOpt.range({
            title: '创建时间',
            startKey: 'startTime',
            endKey: 'endTime',
            check: (val) => {
              if (_this.$tool.dateDiff(val.startTime, val.endTime).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('创建时间跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.startTime, val.endTime).Y >= 2)
            }
          })
        }
      },
      {
        title: '点位数',
        field: 'pointPositionCount',
        minWidth: 150
      },
      {
        title: '扫码数',
        field: 'visitsNum',
        minWidth: 150,
        remoteSort: true
      },
      {
        title: '新增线索客户',
        field: 'newClues',
        minWidth: 150,
        remoteSort: true
      },
      {
        title: '线索转到访',
        field: 'visitCustomer',
        minWidth: 150,
        remoteSort: true
      },
      {
        title: '签订',
        field: 'signCustomer',
        minWidth: 150,
        remoteSort: true
      },
      {
        title: '配置人员',
        field: 'userName',
        minWidth: 150,
        cell: {
          value: (rows) => {
            return `${rows.userName}(${rows.workNo})`
          },
          text: {}
        }
      },
      {
        title: '操作',
        fixed: 'right',
        minWidth: 240,
        align: 'center',
        slots: {
          default: (params, h) => {
            let btns = []
            btns.push({
              title: _this.unlocking('0107100500') ? '活动数据' : '',
              event: 'checkFun',
              data: params.row
            })

            btns.push({
              title: _this.unlocking('0107100200') ? '编辑' : '',
              event: 'editFun',
              data: params.row
            })
            btns.push({
              title: _this.unlocking('0107100700') ? '拓客码' : '',
              event: 'expandFun',
              data: params.row
            })
            btns.push({
              title:
                _this.unlocking('0107100400') &&
                params.row.visitsNum == 0 &&
                params.row.newClues == 0 &&
                params.row.visitCustomer == 0 &&
                params.row.signCustomer == 0
                  ? '删除'
                  : '',
              event: 'delFun',
              data: params.row
            })
            return table.cell.btns(_this, h, btns)
          }
        }
      }
    ]
  }
}

class ColumnSelectUser extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      {
        type: 'checkbox',
        width: 40,
        align: 'center'
      },
      {
        field: 'userName',
        minWidth: 120,
        title: '姓名'
      }
    ]
  }
}

class detailColumn extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      {
        field: 'userName',
        minWidth: 200,
        title: '使用人',
        header: {
          filter: nameOpt.createCounselorPeople({
            key: 'userIds',
            title: '使用人',
            to: (info) => {
              return {
                userIds: info.userId
              }
            }
          })
        },
        cell: {
          value: (rows) => {
            return `${rows.userName}${rows.workNo ? '(' + rows.workNo + ')' : ''}`
          },
          text: {}
        }
      },
      {
        title: '扫码数',
        field: 'visitsNum',
        minWidth: 150,
        remoteSort: true
      },
      {
        title: '新增线索客户',
        field: 'newClues',
        minWidth: 150,
        remoteSort: true
      },
      {
        title: '线索转到访',
        field: 'visitCustomer',
        minWidth: 150,
        remoteSort: true
      },
      {
        title: '签订',
        field: 'signCustomer',
        minWidth: 150,
        remoteSort: true
      }
    ]
  }
}
export default { Column, ColumnSelectUser, detailColumn }
